#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cmsis_os2.h"
#include "ohos_init.h"

#include "iot_uart.h"
#include "UART.h"
#include "utils.h"

#define UART_RX_TASK_STACK_SIZE 1024 * 25
#define UART_RX_TASK_PRIO 23
#define UART_UART_BUFF_SIZE 16
#define UART_UART_STR_SIZE 400
#define UART_UART_Data_SIZE 400
#define UART_FLAGS_MSK1 0x00000001U
#define UART_DTU_UART 2

osThreadAttr_t UART_attr;
osEventFlagsId_t UART_evtID;

uint8_t UART_uartBuff[UART_UART_BUFF_SIZE];
uint8_t * UART_uartBuffPtr = UART_uartBuff;
uint8_t UART_uartStr[UART_UART_STR_SIZE];
uint8_t * UART_uartStrPtr = UART_uartStr;
uint8_t UART_uartData[UART_UART_Data_SIZE];
uint8_t * UART_uartDataPtr = UART_uartData;

// char UART_hello[3] = {0xC3,0xC3,0xC3};
// char * UART_helloPtr = UART_hello;

// char * UART_sockind = "admin,sockind,off\r\n";    // 关闭socket区分
// char * UART_save = "admin,save\r\n";              // 保存配置
// char * UART_sleep = "admin, powermode,0\r\n";     // 取消休眠模式

static int UART_uartLength = 0;
static int UART_uartEndDataLength = 0;

static void *UART_Config_Task(const char *arg){
    (void)arg;

    UART_uartLength = 0;

    while (1) {
        int length = IoTUartRead(UART_DTU_UART, UART_uartBuffPtr, UART_UART_BUFF_SIZE);
        IoTUartWrite(0, UART_uartBuffPtr, length);
        // printf("[DM4GDTU]uart2 length:%d\r\n",length);
        // mm_print_hex(UART_uartBuff,length);

        memcpy((char *)UART_uartStrPtr+UART_uartLength,(char *)UART_uartBuffPtr,length);
        UART_uartLength = UART_uartLength + length;

        if(length < UART_UART_BUFF_SIZE || UART_uartBuff[15] == 0x0A){        // 会话结束
            memcpy((char *)UART_uartDataPtr,(char *)UART_uartStrPtr,UART_uartLength);
            UART_uartEndDataLength = UART_uartLength;
            UART_uartLength = 0;
            UART_sendOSEvent();
            memset(UART_uartStr,0,UART_UART_STR_SIZE);
        }

        memset(UART_uartBuff,0,UART_UART_BUFF_SIZE);
    }

    return NULL;
}

boolean UART_SendHello(void){
    // memset(UART_uartData,0,UART_UART_Data_SIZE);
    // IoTUartWrite(UART_DTU_UART, UART_sockind, strlen(UART_sockind));
    // osDelay(10);
    // IoTUartWrite(UART_DTU_UART, UART_sleep, strlen(UART_sleep));
    // osDelay(10);
    // IoTUartWrite(UART_DTU_UART, UART_save, strlen(UART_save));  
    // osDelay(10);
    // UART_uartEndDataLength = 0;
    // UART_uartLength = 0;
    // memset(UART_uartData,0,UART_UART_Data_SIZE);
    // memset(UART_uartStr,0,UART_UART_STR_SIZE);
    // memset(UART_uartBuff,0,UART_UART_BUFF_SIZE);
    return FALSE;
}

char* UART_SendMsg(char * sendData,int ** getDataLength,int sendDataLength){
    memset(UART_uartData,0,UART_UART_Data_SIZE);
    osEventFlagsClear(UART_evtID,UART_FLAGS_MSK1);
    IoTUartWrite(UART_DTU_UART, sendData, sendDataLength);
    osEventFlagsWait(UART_evtID, UART_FLAGS_MSK1, osFlagsWaitAny, 2000);
    *getDataLength = UART_uartEndDataLength + 0;
    UART_uartEndDataLength = 0;
    return (char*)UART_uartData;
}

void UART_sendOSEvent(void){
    osEventFlagsSet(UART_evtID, UART_FLAGS_MSK1);
    osThreadYield();
}

void UART_ConfigThread(void){
    UART_attr.name = "UART_Config_Task";
    UART_attr.attr_bits = 0U;
    UART_attr.cb_mem = NULL;
    UART_attr.cb_size = 0U;
    UART_attr.stack_mem = NULL;
    UART_attr.stack_size = UART_RX_TASK_STACK_SIZE;
    UART_attr.priority = UART_RX_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)UART_Config_Task, NULL, &UART_attr) == NULL) {
        printf("[NT1ConfigExample] Falied to create NT1Task!\n");
    }

    UART_evtID = osEventFlagsNew(NULL);
}